package com.casic.ops.service.impl;/**
 * @ClassName ops
 * @Description TODO
 * @Author mianbin
 * @Date 2022/6/16 16:24
 * @Version 1.0
 **/

import com.casic.ops.entity.domain.MachineInfoDO;
import com.casic.ops.runner.ssh.SshClient;
import com.casic.ops.runner.ssh.SshClientFactory;
import com.casic.ops.service.api.IAppStoreService;
import com.casic.ops.service.api.MachineEnvService;
import com.casic.ops.service.api.MachineInfoService;
import com.casic.ops.utils.OpsAssert;
import com.casic.ops.utils.ValueMix;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ops
 * @Description app 的商店安装程序,这里主要进行app的安装
 * @Author mianbin
 * @Date 2022/6/16 16:24
 * @Version 1.0
 **/
@Slf4j
@Service("appStoreService")
@RequiredArgsConstructor
public class AppStoreServiceImpl implements IAppStoreService {

    private final MachineInfoService machineInfoService;

    @Override
    public Integer appInstall(String appName, String appVerison, Long machineId) {
        OpsAssert.allNotNull(appName, appVerison);
        MachineInfoDO machineInfoDO = machineInfoService.selectById(machineId);
        OpsAssert.notNull(machineInfoDO);
        try {
            //
            // 这里的操作应该是先用ansible 处理到指定机器,然后操作具体脚本 , 或者执行相关命令.
            //
            SshClient ssh = SshClientFactory.createSsh(machineInfoDO);
            List<String> execs = new ArrayList<>();
            execs.add("cd /home/code/app_install");
            execs.add("docker-compose down");
            execs.add("docker-compose up -d");
            ssh.exec(execs, var -> {
                System.out.println(var);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf("0");
    }

}
